package com.ocom.company.dao;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.ocom.common.entity.company.EntityFlowConfig;
import com.ocom.common.vo.company.FlowInstanceInfoVo;
import com.ocom.common.vo.company.GetFlowInstanceListVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface IDaoFlowConfig extends BaseMapper<EntityFlowConfig> {

    public static String selectSQl =   "(select B.business_id as businessId ,B.com_id as comId,B.def_key as defkey,B.activiti_key as businessKey , B.apply_time as applyTime, P.p_code as pCode , P.p_name as pName ,D.dept_name as deptName , B.title , F.remark as flowRemark , B.status , B.last_approval_user as lastApprover , B.last_approval_time as lastTime " +
            "from biz_apply B " +
            "left join dept_info D on D.dept_id = B.apply_dept_id " +
            "left join personnel_info P on P.p_id = B.p_id  " +
            "left join flow_config F on F.flow_keyword = B.def_key ${ew.customSqlSegment} "
            ;


    @Select("select * from " +
            selectSQl +
            " LIMIT #{limit} OFFSET #{offset} " +  ") as C ")
    List<GetFlowInstanceListVo> getFlowInstanceListVo(Integer offset ,Integer limit ,@Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("select count(*) from " +
            selectSQl +  ") as C " )
    Long getFlowInstanceCount(IPage<GetFlowInstanceListVo> page ,@Param(Constants.WRAPPER) Wrapper wrapper);



    @Select("select P.p_phone as phone,P.p_photo_url2 as photo,B.business_id as businessId,B.def_key as defkey,B.activiti_key as activitiKey , B.apply_time as applyTime, P.p_code as pCode  , P.p_name as pName ,D.dept_name as deptName , B.title , F.remark as flowRemark , B.status ," +
            "L.start_time as startTime ,L.end_time as endTime  " +
            "from biz_apply as B " +
            "left join dept_info D on D.dept_id = B.apply_dept_id " +
            "left join biz_leave_apply L on L.id = B.business_id " +
            "left join personnel_info P on P.p_id = B.p_id " +
            "left join flow_config F on F.flow_keyword = B.def_key  ${ew.customSqlSegment}"
    )
    List<FlowInstanceInfoVo> flowInstanceList(@Param(Constants.WRAPPER) Wrapper wrapper);

}
